const _chrome = chrome;
/**
 * Create a unique key using the getTime() function of the Date instance
 * @returns {number}
 */
export function createSoleKey() {
  return new Date().getTime();
}
/**
 * execute the function in the argument at inspectedWindow
 * @param { the fn can be evaluated } fn
 */
export function inspectedWindowCall(fn) {
  _chrome.devtools.inspectedWindow.eval(fn);
}
/**
 * parse the request and resend it
 * @param { The url of request } url
 * @param { The parameters of request } params
 */
export function _fetch(url, params){
  return new Promise((reslove, reject) => {
    try {
      const fn = createFetchFn(url, params);
      inspectedWindowCall(fn);
      reslove(true);
    } catch (e) {
      reject(e);
    }
  });
}
/**
 * parse out the important apis
 * @param {请求url} url
 * @returns
 */
export function _urlSplit(url) {
  // let reg = new RegExp("^htt(p|ps)://(localhost:\\\d{0,5}|[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\\\d{0,5}|[a-zA-Z0-9]|\\\.?))", "g");
  // return url.replace(reg, "");
  const strArr = url.split("/");
  const length = strArr.length;
  return strArr[length - 1] || strArr[length - 2];
}
/**
 * build url with baseUrl and params
 * @param url
 * @param obj
 * @returns {*}
 */
export function createGetParamsByObj(url, orginObj = {}) {
  if (typeof orginObj !== "object" || Array.isArray(orginObj)) return;
  url += "?";
  const paramsArr = Object.keys(orginObj);
  paramsArr.length > 0 &&
    paramsArr.forEach((key, i) => {
      if (!key) return;
      url += key + "=" + orginObj[key];
      if (i !== paramsArr.length - 1) {
        url += "&";
      }
    });
  return url;
}
/**
 * a key to copy
 * @param { content that needs to be copied } copyContent
 */
export function copy(copyContent) {
  // 1. create and addpend a input element(and then it's destoryed)
  const textareaEle = document.createElement("textarea");
  document.body.appendChild(textareaEle);
  // 2. 将需要复制的文本传入输入框, 并调用 select 方法, 选中输入框中文本
  textareaEle.value = copyContent;
  textareaEle.select();
  textareaEle.readOnly = true;
  // 3. 调用复制选中文本的方法
  document.execCommand("copy");
  // 4. 销毁输入框
  document.body.removeChild(textareaEle);
}

/**
 * create function statement that can be called by inspectedWindow
 * @param { The url of request } url
 * @param { Parameter in the request } parmas
 * @returns
 */
export function createFetchFn(url, parmas) {
  const paramsStr = JSON.stringify(parmas);
  return "fetch(" + "\"" + url + "\"" + ", " + paramsStr + ")";
}

export function cleanRequest(info) {
  return info.params;
}

export function getGetRequestParams(request) {
  const obj = {};
  request?.queryString?.forEach(item => {
    const paramStr = item.value.toString();
    obj[item.name] = decodeURIComponent(paramStr); // unicode decode
  });
  return obj;
}

/**
 * trim the json string
 * @param str json
 * @returns string
 */
function trimJsonStr(str) {
  if (!str) return "";
  return str.replace(/\r\n/g, "").replace(/\n/g, "").replace(/\s+/g, "");
}
